<?php

namespace app\hotel\controller;

use think\Controller;

class Guest extends Controller
{
  // 继承底层构造函数
  public function __construct()
  {
    parent::__construct();

    $this->BusinessModel = model('Business.Business');

    $this->model = model('Hotel.Guest');

    // 接收传递过来的busid
    $this->busid = $this->request->param('busid', 0, 'trim');
    // 查找用户是否存在
    $this->business = $this->BusinessModel->find($this->busid);

    // 判断
    if (!$this->business) {
      $this->error('用户不存在');
      exit;
    }
  }

  // 住客信息列表
  public function index()
  {
    // 接收参数
    $page = $this->request->param('page', 1, 'trim');
    $limit = 10;
    $start = ($page - 1) * $limit;

    $list = $this->model
      ->where(['busid' => $this->busid])
      ->order('id desc')
      ->limit($start, $limit)
      ->select();

    if ($list) {
      $this->success('住客信息', null, $list);
      exit;
    } else {
      $this->error('没有更多住客信息噜~~');
      exit;
    }
  }

  // 添加住客信息
  public function add()
  {
    if ($this->request->isPost()) {
      //获取所有的参数
      $busid = $this->request->param('busid', 0, 'trim');
      $nickname = $this->request->param('nickname', 0, 'trim');
      $gender = $this->request->param('gender', 0, 'trim');
      $mobile = $this->request->param('mobile', 0, 'trim');

      // 组装数据
      $data = [
        'busid' => $busid,
        'nickname' => $nickname,
        'gender' => $gender,
        'mobile' => $mobile,
      ];

      // 插入语句
      $result = $this->model->save($data);


      if ($result === FALSE) {
        $this->error('添加住客信息失败');
        exit;
      } else {
        $this->success('添加成功');
        exit;
      }
    }
  }

  // 住客信息的数据
  public function info()
  {
    if ($this->request->isPost()) {
      $id = $this->request->param('id', 0, 'trim');

      $guest = $this->model->find($id);

      if ($guest === FALSE) {
        $this->error('暂无住客信息');
        exit;
      } else {
        $this->success('返回住客信息', null, $guest);
        exit;
      }
    }
  }

  // 编辑住客信息
  public function edit()
  {
    if ($this->request->isPost()) {
      //获取所有的参数
      $busid = $this->request->param('busid', 0, 'trim');
      $nickname = $this->request->param('nickname', 0, 'trim');
      $gender = $this->request->param('gender', 0, 'trim');
      $mobile = $this->request->param('mobile', 0, 'trim');
      $id = $this->request->param('id', 0, 'trim');

      // 组装数据
      $data = [
        'id' => $id,
        'busid' => $busid,
        'nickname' => $nickname,
        'gender' => $gender,
        'mobile' => $mobile,
      ];

      //先判断住客信息是否存在
      $guest = $this->model->find($id);

      if (!$guest) {
        $this->error('住客信息不存在');
        exit;
      }

      $result = $this->model->isUpdate(true)->save($data);

      if ($result === FALSE) {
        $this->error('编辑住客信息失败');
        exit;
      } else {
        $this->success('编辑成功');
        exit;
      }
    }
  }

  // 删除住客信息
  public function del()
  {
    if ($this->request->isPost()) {
      $id = $this->request->param('id', 0, 'trim');

      $guest = $this->model->find($id);

      if ($guest === FALSE) {
        $this->error('暂无住客信息');
        exit;
      }

      $result = $this->model->where(['id' => $id])->delete();

      if ($result === FALSE) {
        $this->error('删除住客信息失败');
        exit;
      } else {
        $this->success('删除住客信息成功');
        exit;
      }
    }
  }
}
